# -*- coding: utf-8 -*-
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, render, redirect
from django.template.context import RequestContext
from django.utils.translation import ugettext_lazy as _
from common.models import Address
from companies.models import Company, Company_Area, Company_Industry
from countries_states.models import Country, State
from companies.forms import CompanyForm
from common.forms import AdressForm

from userprofiles import settings as up_settings
from userprofiles.utils import get_form_class
from userprofiles.contrib.profiles.forms import ProfileForm


@login_required
def profile(request):
    return render_to_response('userprofiles/profile.html', {
        'user': request.user,
    }, context_instance=RequestContext(request))


@login_required
def profile_change(request):
    # ProfileForm = get_form_class(up_settings.PROFILE_FORM)
    user = request.user
    company = Company.objects.get(user_id=user.pk)
    address = Address.objects.get(pk=company.address_id)
    logo = company.logo
    if request.method == 'POST':
        form_user = ProfileForm(request.POST, request.FILES, instance=user)
        try:
            country_selected = Country.objects.get(id= request.POST['country'])
        except:
            country_selected = None
        try:
            if int(request.POST['state']) == 0:
                state_selected = 0
            else:
                state_selected = State.objects.get(id= request.POST['state'])
        except:
            state_selected = None
        form_address = AdressForm(request.POST, request.FILES, instance=address,
                                  country_selected=country_selected,
                                  state_selected=state_selected,)
        try:
            company_area_selected = Company_Area.objects.get(pk=int(request.POST['company_area']))
        except:
            company_area_selected = None
        try:
            if int(request.POST['company_industry']) == 0:
                company_industry_selected = 0
            else:
                company_industry_selected = Company_Industry.objects.get(id=request.POST['company_industry'])
        except:
            company_industry_selected = None
        form_company = CompanyForm(request.POST, request.FILES, instance=company,
                                    company_area_selected=company_area_selected,
                                    company_industry_selected=company_industry_selected,)
        if form_user.is_valid() and form_company.is_valid() and form_address.is_valid():
            user = form_user.update()
            address = form_address.update()
            company = form_company.update(address=address)
            messages.success(request, _(u'Hemos modificado sus datos con éxito'))
            return redirect(up_settings.PROFILE_CHANGE_DONE_URL)

        # form = ProfileForm(request.POST,
        #                    request.FILES,
        #                    country_selected=country_selected,
        #                    state_selected=state_selected,
        #                    instance=request.user.get_profile()
        # )
        # if form.is_valid():
        #     profile = form.save()
        #     messages.success(request, _(u'Hemos modificado sus datos con éxito'))
        #     return redirect(up_settings.PROFILE_CHANGE_DONE_URL)
    else:
        form_user = ProfileForm(instance=request.user)
        form_company = CompanyForm(instance=company, company_area_selected=company.company_area,
                                   company_industry_selected=company.company_industry, change_profile=True,
                                   initial={'company_phone': company.phone})
        form_address = AdressForm(instance=address, country_selected=address.country,
                                  state_selected=address.state, change_profile=True)
    pageheader = _(u'MODIFICAR MI PERFIL')
    return render(request,
                  'userprofiles/registration.html',
                  {'form_user': form_user,
                   'form_company': form_company,
                   'form_address': form_address,
                   'pageheader': pageheader,
                   'logo': logo
                  })
